package com.hp.team.bookstore.dbutil.init;

import com.hp.team.bookstore.dbutil.dao.DAOFactory;
import com.hp.team.bookstore.dbutil.dao.HibernateUtil;
import com.hp.team.bookstore.dbutil.dao.catalog.VendorHibernateDAO;
import com.hp.team.bookstore.dbutil.dao.order.PORequirementHibernateDAO;
import com.hp.team.bookstore.dbutil.dao.order.SalesOrderItemHibernateDAO;
import com.hp.team.bookstore.model.catalog.Vendor;
import com.hp.team.bookstore.model.order.PORequirement;
import com.hp.team.bookstore.model.order.SalesOrderItem;

public class PORequirementInitializer {

	private static PORequirementHibernateDAO poRequirementDAO;

	private static SalesOrderItemHibernateDAO orderItemDAO;

	private static VendorHibernateDAO vendorDAO;

	static {
		DAOFactory daoFactory = new DAOFactory();
		poRequirementDAO = daoFactory.getPORequirementDAO();
		orderItemDAO = daoFactory.getSalesOrderItemDAO();
		vendorDAO = daoFactory.getVendorDAO();
	}

	public static void init() {

		poRequirementDAO.setSession(HibernateUtil.getSessionFactory().getCurrentSession());
		HibernateUtil.beginWriteTransaction();

		for (int i = 0; i < 10; i++) {
			PORequirement poRequirement = createPORequirement(i);
			poRequirementDAO.makePersistent(poRequirement);
		}

		HibernateUtil.commitTransaction();
	}

	public static PORequirement createPORequirement(long id) {

		PORequirement poRequirement = new PORequirement();

		Vendor vendor = vendorDAO.findById(id + 11, false);
		poRequirement.setVendor(vendor);

		SalesOrderItem orderItem = orderItemDAO.findById(id + 11, false);
		poRequirement.setOrderItem(orderItem);

		return poRequirement;
	}

}
